using Autofac;
using Autofac.Extensions.DependencyInjection;
using CoreProject.Api;
using CoreProject.Model;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
using Microsoft.OpenApi.Models;
using NLog.Web;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(
  c => {
    c.AddSecurityDefinition("Bearer",  //定义授权方案的名称
               new OpenApiSecurityScheme()
               {
                 Description = "请输入token：Authorization:{token}",  //描述文字
                 Name = "Authorization",  //参数名--与标题头的参名相同
                 In = ParameterLocation.Header,  //参数放在Header中
                 Type = SecuritySchemeType.ApiKey,  //类型是apikey
               });
    //加载授权方案
    c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"  //授权方案名称
                            }
                        },
                        new string[] { }
                    }
                });
  }
);

//注册Cors 跨域
builder.Services.AddCors(options =>
{
  options.AddPolicy("AllowCors", builder =>
  {
    builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
  });
});

//注册Jwt
builder.Services.AddAuthorizationSetup(builder.Configuration);

//注册EF 数据上下文
builder.Services.AddDbContext<EFDbContext>(options =>
{
  var connection = builder.Configuration.GetConnectionString("DefaultConnection");
  options.UseSqlServer(
      connection
  );
});

builder.Services.AddStackExchangeRedisCache(options =>
{
  options.Configuration = builder.Configuration["RedisGroup:Host"];
  options.InstanceName = builder.Configuration["RedisGroup:InstanceName"];
});


//3.在 redis 分布式缓存 IOC 容器中注册服务
builder.Services.AddStackExchangeRedisCache(options =>
{
  options.Configuration = builder.Configuration["RedisGroup:Host"];
  options.InstanceName = builder.Configuration["RedisGroup:InstanceName"];
});
// 以下是autofac依赖注入
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
{
  //批量注册服务  注册模块
  builder.RegisterModule(new CustomAutofacModule());
});



// NLog：注册Nlog组件
builder.Logging.ClearProviders();
builder.Host.UseNLog();

//添加对AutoMapper的支持，会查找所有程序集中继承了 Profile 的类
builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

var app = builder.Build();


//异常处理中间件
app.UseMiddleware(typeof(ExceptionHandlerMiddleWare));

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
  app.UseSwagger();
  app.UseSwaggerUI();
}

app.UseCors("AllowCors");

//身份验证
app.UseAuthentication();
//授权
app.UseAuthorization();

//自定义一个静态文件访问的中间件
app.UseStaticFiles(new StaticFileOptions()
{
  FileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "Static/HeadImg")),
  RequestPath = "/Static/HeadImg"
});

//自定义一个静态文件访问的中间件
app.UseStaticFiles(new StaticFileOptions()
{
  FileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "Static/UploadFile")),
  RequestPath = "/Static/UploadFile"
});

//自定义一个静态文件访问的中间件
app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(app.Environment.ContentRootPath, "Static/picture")),
    RequestPath = "/static/picture"
});


app.MapControllers();

app.Run();



